; Exercise 1.15.
; The sine of an angle (specified in radians) can be computed by making use of the approximation 
; sin x = x if x is sufficiently small, and the trigonometric identity
;
; sin r = 3sin(r/3) - 4sin^3(r/3)
; to reduce the size of the argument of sin. 
;
; (For purposes of this exercise an angle is considered ``sufficiently small'' if
; its magnitude is not greater than 0.1 radians.) 
; These ideas are incorporated in the following procedures:

(define (abs x)
  (if (< x 0)
      (- x)
      x))

(define (cube x) (* x x x))

(define (p x)
  (- (* 3 x) (* 4 (cube x))))

(define (sine angle)
  (if (> (abs angle) 0.1)
      (p (sine (/ angle 3.0)))
      angle))

; a. How many times is the procedure p applied when (sine 12.15) is evaluated?
; b. What is the order of growth in space and number of steps (as a function of a)
;    used by the process generated by the sine procedure when (sine a) is evaluated?

; a.
;
; Although its definition is recursive it is in fact executing linearly iterating
; since it is calculating the value of sine only once per call to defined procedure
; p (applicative substitution which works on the principle 'evaluate operator 
; and the operands and then apply operators to operands, eval -> apply').
;
; Taking 0.1 as base for approximation it executes 5 calls to the p procedure.

(display (sine 12.15))
(newline)


; b.
;
; Size of the problem is defined by the value of the angle we can see following
; for the:
;
; time:
;   Angle is in every step reduced three times (r/3) so it must be somehow logarithmic
;   with the base of 3? O(log3(n))
;
; space:
;   Every time p is called it has t remember 2 values for future calculation. So it is
;   2*n for n calls. Since calls are growing logarithmicaly space is as well, just a bit faster
;
